/**
 * 四海兄弟工作小组，群号:160517807
 * @author 乔继承
 * @date 2014年1月18日 下午12:55:58 
 * @version V1.0   
 */
package org.mcpro.common.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mcpro.common.dao.ICommonObjectDAO;
import org.mcpro.common.util.ObjectUtil;
import org.mcpro.core.dao.impl.BaseDAOImpl;
import org.mcpro.core.model.DictObject;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

/**
 * 
 * @author 乔继承
 * @date 2014年1月18日 下午12:55:58
 * 
 */
public class CommonObjectDAOImpl extends BaseDAOImpl implements
		ICommonObjectDAO {
	private Logger logger = Logger.getLogger(this.getClass());

	public List<DictObject> getDictObjectList(String... ids) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT dobj.id,dobj.sobjectName,dobj.suiName,dobj.stableName,dobj.snote FROM dict_object dobj");
		if (ObjectUtil.isNotEmpty(ids)) {
			sql.append(" where dobj.id in(");
			int i = 1;
			int length = ids.length;
			for (String id : ids) {
				sql.append("'").append(id).append("'");
				if (length != i++) {
					sql.append(",");
				}
			}
			sql.append(")");
		}
		JdbcTemplate jdbcTemplate = new JdbcTemplate(this.getDataSource());
		final List<DictObject> list = new ArrayList<DictObject>();
		jdbcTemplate.query(sql.toString(), new RowCallbackHandler() {
			DictObject dictObject = null;

			@Override
			public void processRow(ResultSet rs) throws SQLException {
				do {
					String id = rs.getString("id");
					String sobjectName = rs.getString("sobjectName");
					String suiName = rs.getString("suiName");
					String stableName = rs.getString("stableName");
					String snote = rs.getString("snote");
					dictObject = new DictObject(id, sobjectName, suiName,
							snote, stableName);
					list.add(dictObject);
				} while (rs.next());
			}

		});
		return list;
	}

	/**
	 * @param id
	 * @return
	 * @see org.mcpro.common.dao.ICommonObjectDAO#getTableAndField(java.lang.String)
	 */
	@Override
	public DictObject getTableAndField(String id) {
		if (ObjectUtil.isEmpty(id)) {
			return null;
		}
		String sql = "SELECT dobj.stableName,dobj.sobjectName,dobj.suiName,dof.sfieldName "
				+ "FROM dict_object dobj JOIN dict_object2field dof ON dobj.id=dof.idObject "
				+ "AND dobj.id=?";
		final DictObject dictObject = new DictObject();
		dictObject.setId(id);
		JdbcTemplate jdbcTemplate = new JdbcTemplate(this.getDataSource());
		jdbcTemplate.query(sql.toString(), new Object[] { id },
				new RowCallbackHandler() {

					@Override
					public void processRow(ResultSet rs) throws SQLException {
						List<String> list = dictObject.getFields();
						String stableName = rs.getString("stableName");
						String sobjectName = rs.getString("sobjectName");
						String suiName = rs.getString("suiName");
						do {
							String sfieldName = rs.getString("sfieldName");
							list.add(sfieldName);
						} while (rs.next());
						dictObject.setStableName(stableName);
						dictObject.setStableName(sobjectName);
						dictObject.setSuiName(suiName);
					}

				});
		return dictObject;
	}

	@Override
	public DictObject getData(DictObject dictObject) {
		String stableName = dictObject.getStableName();
		final List<String> fields = dictObject.getFields();

		if (ObjectUtil.isEmpty(dictObject) || ObjectUtil.isEmpty(stableName)
				|| ObjectUtil.isEmpty(fields)) {
			return null;
		}
		int length = fields.size();
		int i = 1;
		StringBuffer sql = new StringBuffer();
		StringBuffer sqlCondition = new StringBuffer();
		for (String f : fields) {
			if (ObjectUtil.isNotEmpty(f)) {
				if (i < length) {
					sqlCondition.append("obj.").append(f).append(",");
				} else {
					sqlCondition.append("obj.").append(f);
				}
			}
			i++;
		}
		final List<Map<String, String>> listMaps=dictObject.getDictObjectValues(); 
		sql.append("SELECT ").append(sqlCondition).append(" FROM `")
				.append(stableName).append("` obj");
		JdbcTemplate jdbcTemplate = new JdbcTemplate(this.getDataSource());
		jdbcTemplate.query(sql.toString(), new RowCallbackHandler() {

			@Override
			public void processRow(ResultSet rs) throws SQLException {
				Map<String, String> map=null;
				String value=null;
				do {
					map=new HashMap<String, String>();
					for(String f:fields){
						value=rs.getString(f);//to do测试方法，没有判断数据库类
						map.put(f, value);
					}
					listMaps.add(map);
				} while (rs.next());
			}

		});
		return dictObject;
	}
}
